package com.caigou.util;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author matthew
 * 实现文件exif属性操作工具类
 * @version 1.0.0
 * @date 2020-02-17 21:46:30
 */
@Slf4j
public class ExifUitl {


    public static String[] readExif(File file) throws ImageProcessingException, IOException {
        String[] array = new String[3];
        //如果你对图片的格式有限制，可以直接使用对应格式的Reader如：JPEGImageReader
        ImageMetadataReader.readMetadata(file)
                .getDirectories().forEach(v ->
                v.getTags().forEach(t -> {
                    //log.info(t.getTagName() + " ： " + t.getDescription());
                    switch (t.getTagName()) {
//                        //经度
//                        case "GPS Longitude":
//                            array[0] = t.getDescription();
//                            break;
//                        //纬度
//                        case "GPS Latitude":
//                            array[1] = t.getDescription();
//                            break;
                        //图片宽度
                        case "Image Width":
                            array[0] = t.getDescription();
                            break;
                        //图片高度
                        case "Image Height":
                            array[1] = t.getDescription();
                            break;
                        //拍摄时间
                        case "Date/Time Original":
                            array[2] = t.getDescription();
                            break;
                        default:
                            break;
                    }
                })
        );
        return array;
    }

    /**
     * 获取图片拍摄时间
     * @param file
     * @return
     * @throws ImageProcessingException
     * @throws IOException
     */
    public static String readTime(File file) throws ImageProcessingException, IOException{
        String photoTime = null;
        Metadata metadata = ImageMetadataReader.readMetadata(file);
        Iterator<Directory> it = metadata.getDirectories().iterator();
        while(it.hasNext()){
            Directory directory = it.next();
            Iterator<Tag> tags = directory.getTags().iterator();
            while(tags.hasNext()){
                Tag tag = tags.next();
                String tagName = tag.getTagName();
//                log.info("tagName={}, description={}", tagName, tag.getDescription());
//                if(tagName.equals("Width")){
//                    log.info("Width={}", tag.getDescription());
//                }
//                if(tagName.equals("Height")){
//                    log.info("Height={}", tag.getDescription());
//                }
                if(tagName.equals("Date/Time Original")){//拍摄时间
                    photoTime = tag.getDescription();
                    log.info("photoTime={}", photoTime);
                    return photoTime;
                }
            }
        }
        return photoTime;
    }
}